package mashuaiproject.mashuaiproject.controller;

import mashuaiproject.mashuaiproject.mapper.UserMapper;
import mashuaiproject.mashuaiproject.model.Quesions;
import mashuaiproject.mashuaiproject.model.Users;
import mashuaiproject.mashuaiproject.serivce.JMontiorSerivce;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class LoginRegistContrller {
    @Autowired
    JavaMailSender mailSender;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JMontiorSerivce jMontiorSerivce;
    @GetMapping("/loginRegist")
    public  String loginregist(){

        return "logres";
    }

    //登录
    @ResponseBody
    @PostMapping("/loginUser")
    public  String loginUser(@RequestParam("loginEmail")String loginEmail,
                             @RequestParam("loginPass") String loginPass,
                             HttpServletResponse response){
      //判断账号面膜是否正确：
        String password=userMapper.findUsersPass(loginEmail);
        if(password!=null){
            if(password.equals(loginPass)){
                //找出当前的token
                String token=userMapper.findUserToken(loginEmail);
                response.addCookie(new Cookie("token",token));//写入cokien

                return  "succes";
            }
        }

        return "faileds";
    }


    //发送邮箱验证
    @ResponseBody
    @GetMapping("/sendEmail")
    public Map<String,Object> sendEmail(@RequestParam("account_id")String account_id,@RequestParam("name")String name){
        Map<String ,Object> map=new HashMap<>();
        //首先判断账号或者邮箱是否已经存在
        int judes=userMapper.checkUser2(account_id,name);
        if(judes!=0){
            //已经存在的账号或者邮箱
            map.put("vectiy","hasUser");
        }else{

            try {
                final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
                final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                message.setFrom("937958191@qq.com");
                message.setTo(account_id);
                message.setSubject("欢迎你注册XXX社区：");
                int vectiy=(int)((Math.random()*9+1)*100000);
                System.err.println("验证码："+vectiy);
                message.setText("你的验证码是："+vectiy+"，请输入有效的验证码进行注册");
                this.mailSender.send(mimeMessage);
                map.put("vectiy",vectiy);


            } catch (Exception ex) {
                map.put("vectiy",null);
                ex.printStackTrace();
            }
        }



        return  map;
    }

    //插入用户信息；
    @ResponseBody
    @GetMapping("/insertUser")
    public  String insertUser(@RequestParam("account_id")String account_id,
                              @RequestParam("password")String password,
                              @RequestParam("name") String name,
                              HttpServletResponse response){
        Users users=new Users();
        users.setAccount_id(account_id);
        users.setName(name);
        users.setPassword(password);
        String token= UUID.randomUUID().toString();
        users.setToken(token);
        users.setGmt_create(System.currentTimeMillis());
        users.setGmt_modified(users.getGmt_create());
        users.setAvatarUrl("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3789621230,3191440928&fm=26&gp=0.jpg");//给于一个默认的头像
        //插入用户信息
        userMapper.insertUser2(users);
        response.addCookie(new Cookie("token",token));//写入cokien

        //新用户给于一个勋章
        Users users1=userMapper.findByToken(token);
        int insertSelfXunzhang=userMapper.insertSelfXunzhang(users1.getId());
        //更新昵称：
        int updateChengwei=userMapper.upateChenWei(users1.getId(),"萌新");
        System.err.println("勋章更新情况："+updateChengwei);
        return "successs";
    }

}
